<?php
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}
unset($_SESSION['searchstring']);
$rows_per_page = 30;
$page_start = ( $page - 1 ) * $rows_per_page;
$page_end = $page * $rows_per_page;

$order_by = "order by SUBSTRING_INDEX(e.FullName, ' ', -1) ASC";
$_SESSION['searchstring'] = $order_by;

$str = "";
if ($_SESSION['db'] == 'elitech') {
    $str = "AND e.Type_employee=1 ";
}
$sqlempworking = mysql_query("SELECT e.IDEmployee,e.FullName,DATE_FORMAT(e.Birthday,'%d/%m/%Y') as Birthday,e.UserName,al.SexName,dl.DepartmentName,e.Position,e.StartDate,e.Address,e.CMND,e.Mobile 
							FROM employee e ,department_lang dl, ac_sex_lang al, roles rl 
						   where e.IDDepartment = dl.IDDepartment and dl.LangID = '" . $_SESSION['lang'] . "' 
						   and e.Sex = al.SexID and al.LangID = '" . $_SESSION['lang'] . "'  
						   and e.IDRole= rl.IDRole and e.IDRole != 1 and e.State = 1 $str" . $order_by );

$number_of_page = ceil(mysql_num_rows($sqlempworking) / $rows_per_page);
if (isset($_REQUEST['searchAll'])) {
    //$_SESSION['searchstring'] = '';
    unset($_SESSION['searchstring']);
}
function ViPAencode($text) {
    $origin = array(
        '/([0-9])/',
        '/a/', '/à/', '/ả/', '/ã/', '/á/', '/ạ/',
        '/ă/', '/ằ/', '/ẳ/', '/ẵ/', '/ắ/', '/ặ/',
        '/â/', '/ầ/', '/ẩ/', '/ẫ/', '/ấ/', '/ậ/',
        '/e/', '/è/', '/ẻ/', '/ẽ/', '/é/', '/ẹ/',
        '/ê/', '/ề/', '/ể/', '/ễ/', '/ế/', '/ệ/',
        '/i/', '/ì/', '/ỉ/', '/ĩ/', '/í/', '/ị/',
        '/o/', '/ò/', '/ỏ/', '/õ/', '/ó/', '/ọ/',
        '/ô/', '/ồ/', '/ổ/', '/ỗ/', '/ố/', '/ộ/',
        '/ơ/', '/ờ/', '/ở/', '/ỡ/', '/ớ/', '/ợ/',
        '/u/', '/ù/', '/ủ/', '/ũ/', '/ú/', '/ụ/',
        '/ư/', '/ừ/', '/ử/', '/ữ/', '/ứ/', '/ự/',
        '/y/', '/ỳ/', '/ỷ/', '/ỹ/', '/ý/', '/ỵ/',
        '/A/', '/À/', '/Ả/', '/Ã/', '/Á/', '/Ạ/',
        '/Ă/', '/Ằ/', '/Ẳ/', '/Ẵ/', '/Ắ/', '/Ặ/',
        '/Â/', '/Ầ/', '/Ẩ/', '/Ẫ/', '/Ấ/', '/Ậ/',
        '/E/', '/È/', '/Ẻ/', '/Ẽ/', '/É/', '/Ẹ/',
        '/Ê/', '/Ề/', '/Ể/', '/Ễ/', '/Ế/', '/Ệ/',
        '/I/', '/Ì/', '/Ỉ/', '/Ĩ/', '/Í/', '/Ị/',
        '/O/', '/Ò/', '/Ỏ/', '/Õ/', '/Ó/', '/Ọ/',
        '/Ô/', '/Ồ/', '/Ổ/', '/Ỗ/', '/Ố/', '/Ộ/',
        '/Ơ/', '/Ờ/', '/Ở/', '/Ỡ/', '/Ớ/', '/Ợ/',
        '/U/', '/Ù/', '/Ủ/', '/Ũ/', '/Ú/', '/Ụ/',
        '/Ư/', '/Ừ/', '/Ử/', '/Ữ/', '/Ứ/', '/Ự/',
        '/Y/', '/Ỳ/', '/Ỷ/', '/Ỹ/', '/Ý/', '/Ỵ/',
        '/d/', '/đ/', '/D/', '/Đ/');

    $codes = array(
        "-$1",
        'a0', 'a02', 'a03', 'a04', 'a05', 'a06',
        'a1', 'a12', 'a13', 'a14', 'a15', 'a16',
        'a2', 'a22', 'a23', 'a24', 'a25', 'a26',
        'e0', 'e02', 'e03', 'e04', 'e05', 'e06',
        'e1', 'e12', 'e13', 'e14', 'e15', 'e16',
        'i0', 'i02', 'i03', 'i04', 'i05', 'i06',
        'o0', 'o02', 'o03', 'o04', 'o05', 'o06',
        'o1', 'o12', 'o13', 'o14', 'o15', 'o16',
        'o2', 'o22', 'o23', 'o24', 'o25', 'o26',
        'u0', 'u02', 'u03', 'u04', 'u05', 'u06',
        'u1', 'u12', 'u13', 'u14', 'u15', 'u16',
        'y0', 'y02', 'y03', 'y04', 'y05', 'y06',
        'A0', 'A02', 'A03', 'A04', 'A05', 'A06',
        'A1', 'A12', 'A13', 'A14', 'A15', 'A16',
        'A2', 'A22', 'A23', 'A24', 'A25', 'A26',
        'E0', 'E02', 'E03', 'E04', 'E05', 'E06',
        'E1', 'E12', 'E13', 'E14', 'E15', 'E16',
        'I0', 'I02', 'I03', 'I04', 'I05', 'I06',
        'O0', 'O02', 'O03', 'O04', 'O05', 'O06',
        'O1', 'O12', 'O13', 'O14', 'O15', 'O16',
        'O2', 'O22', 'O23', 'O24', 'O25', 'O26',
        'U0', 'U02', 'U03', 'U04', 'U05', 'U06',
        'U1', 'U12', 'U13', 'U14', 'U15', 'U16',
        'Y0', 'Y02', 'Y03', 'Y04', 'Y05', 'Y06',
        'd81', 'd91', 'D81', 'D91');

    $encoded = preg_replace($origin, $codes, $text);
    return $encoded;
}

function ViSort($list) { // $list must be an array
    for ($i = 0; $i < count($list); $i++) {
        $list_encoded[$i] = ViPAencode($list[$i]);
        $list_encoded[$i] = preg_replace("/\b(\w+)([012])([2-6])(\w+)?\b/", "$1$2$4$3", $list_encoded[$i]);
    }
    // Convert to lowercase
    $list_encoded_lowercase = array_map('strtolower', $list_encoded);
    // Sort list by encoded list (in lowercase),
    // so we don't have to write the crazy function called 'ViPAdecode' :p 
    array_multisort($list_encoded_lowercase, SORT_ASC, SORT_STRING, $list);

    return $list;
}

function ViSortByName($list) { // $list must be an array
// TODO: The priority must be checked again!
// There are some related articles on Journal of Language
// (printed about 5 years ago)...
    for ($i = 0; $i < count($list); $i++) {
        $list_reversed = explode(" ", trim($list[$i]));
        $list_reversed = array_reverse($list_reversed);
        $list[$i] = implode(" ", $list_reversed);
    }

    $list_sorted = ViSort($list);

    for ($j = 0; $j < count($list_sorted); $j++) {
        $list_unreversed = explode(" ", $list_sorted[$j]);
        $list_unreversed = array_reverse($list_unreversed);
        $list_sorted[$j] = implode(" ", $list_unreversed);
    }

    return $list_sorted;
}
?>
<script type="text/javascript">

    function sortFullname(){
        var orderby = $('input#sortfullname').val();	
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        $.ajax({url:"admin/employeemanagement/ajaxpagingsortworking.php", data : {orderby : orderby , flags : 'fullname', l_edit : l_edit, l_delete : l_delete}, success:function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
                if (orderby==<?= SORT_ASC ?>){
                    $('input#sortfullname').val(<?= SORT_DESC ?>);
                }else{
                    $('input#sortfullname').val(<?= SORT_ASC ?>);
                }
            }});
    }

    function sortUsername(){
        var orderby = $('input#sortusername').val();	
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        $.ajax({url:"admin/employeemanagement/ajaxpagingsortworking.php", data : {orderby : orderby , flags : 'username', l_edit : l_edit, l_delete : l_delete}, success:function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
                if (orderby==<?= SORT_ASC ?>){
                    $('input#sortusername').val(<?= SORT_DESC ?>);
                }else{
                    $('input#sortusername').val(<?= SORT_ASC ?>);
                }
            }});
    }

    function paging(page, search_str){
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        $.ajax({url:"admin/employeemanagement/ajaxpagingemployeeworking.php", data : {pages : page , l_edit : l_edit, l_delete : l_delete}, success:function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
            }});
    }

    function searching(page, eleID){ 
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        var typeSearch = $('select#typeSearch').val();
        var search_s = $('input#fullname').val();
        $.ajax({url:"admin/employeemanagement/ajaxsearchemployeeworking.php", data : {pages : page, typeSearch : typeSearch , search_s : search_s ,  l_edit : l_edit, l_delete : l_delete}, success:function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
            }});
    }

    function searchingAuto(page, eleID){ 
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        if($('input#flags').val()==''){
            $('input#flags').val('processing');
            var typeSearch = $('select#typeSearch').val();
            var search_s = $('input#fullname').val();
            $.ajax({url:"admin/employeemanagement/ajaxsearchemployeeworking.php", data : {pages : page, typeSearch : typeSearch , search_s : search_s, l_edit : l_edit, l_delete : l_delete }, success:function(result){
                    $("table#listEmp tr:not(:first)").remove();
                    $("table#listEmp thead").append(result);
                    if($('input#flags').val()=='waiting'){
                        $('input#flags').val('');
                        searchingAuto(1,'');								
                    }
                    else{
                        $('input#flags').val('');
                    }
                }});
        }else{
            $('input#flags').val('waiting');
        }
    }

    function searchingAll(page, search_str){
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        $('input#fullname').val('');
        $.ajax({url:"admin/employeemanagement/ajaxpagingemployeeworking.php", data : {pages : page, searchAll : search_str,  l_edit : l_edit, l_delete : l_delete}, success:function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
            }});
    }

    function onclickEdit(){
	
    }
    function onclickDelete(idemployee,els){	
        var l_edit = $('input#l_edit').val();
        var l_delete = $('input#l_delete').val();
        $.ajax({url:"admin/employeemanagement/process-delete-employee-working.php", data : {idemployee : idemployee ,  l_edit : l_edit, l_delete : l_delete}, success : function(result){
                $("table#listEmp tr:not(:first)").remove();
                $("table#listEmp thead").append(result);
            }});
	
    }
    function onexportexcel(){
        var str = "printer/exportexcel_employee_working.php?";
        var gt=document.getElementById("check_no").checked;
        if(gt==true)
            str=str+"&check_no=on";
        var gt=document.getElementById("check_employee_name").checked;
        if(gt==true)
            str=str+"&check_employee_name=on";
        var gt=document.getElementById("check_birthday").checked;
        if(gt==true)
            str=str+"&check_birthday=on";
        var gt=document.getElementById("check_username").checked;
        if(gt==true)
            str=str+"&check_username=on";
        var gt=document.getElementById("check_sex").checked;
        if(gt==true)
            str=str+"&check_sex=on";
        var gt=document.getElementById("check_department").checked;
        if(gt==true)
            str=str+"&check_department=on";
        var gt=document.getElementById("check_position").checked;
        if(gt==true)
            str=str+"&check_position=on";
        var gt=document.getElementById("check_startwork").checked;
        if(gt==true)
            str=str+"&check_startwork=on";
        var gt=document.getElementById("check_cmnd").checked;
        if(gt==true)
            str=str+"&check_cmnd=on";
        var gt=document.getElementById("check_address").checked;
        if(gt==true)
            str=str+"&check_address=on";
        var gt=document.getElementById("check_mobile").checked;
        if(gt==true)
            str=str+"&check_mobile=on";
        var gt=document.getElementById("check_totaldays").checked;
        if(gt==true)
            str=str+"&check_totaldays=on";
	
        window.open(str);
	
    }
</script>
<script type="text/javascript">
    $(document).ready(function(){
<?php if (isset($_SESSION['successus'])) { ?>
                    alert("<?php echo $l_themthanhcong ?>");
    <?php unset($_SESSION['successus']);
} ?>


            });

</script>
<div id="contains" style="background-color: #edf4f7;">
    <div class="title">
        <div
            style="color: #0962C8; font-size: 12px; font-weight: bold; padding-left: 2px; padding-top: 8px; text-transform: uppercase;"></div>
    </div>
    <div class="body" style="margin-left: 5px;margin-right:5px" id="contentEmpWorking">

        <div style="float: left; width: 300px; margin: 0px 5px 5px 0px;">
            <a href="create-new-account-admin.php">
                <input id="createaccount" name="createaccount" value="<?= $l_createaccount ?>"
                       class="buttonNormal" style="width: 140px;" type="submit" /></a>
            <a onclick="onexportexcel()"><input type="button" value="<?= $l_export_excel ?>" class="buttonNormal" style="width: 140px;"/></a>
            <input type="hidden" value="<?= $l_edit ?>" id="l_edit"/>	
            <input type="hidden" value="<?= $l_delete ?>" id="l_delete"/>		
        </div>
        <div
            style="float: right; width: 520px; text-align: right; margin: 0px 5px 5px 0px;">
            <select id="typeSearch" name="typeSearch" size="1"
                    style="width: 120px;height: 21px;">
                <option value="fullname"><?= $l_fullname ?></option>
                <option value="username"><?= $l_username ?></option>				
            </select> <i></i>
            <input type="hidden" id="flags"/>
            <input id="fullname" name="fullname" type="text" onkeyup="searchingAuto(1,'')"
                   value="" style="width: 177px;" class="textfiledCompany" /> <i></i>
            <input id="search" name="search" type="button" value="<?= $l_search ?>" onclick="searching(1,'')"
                   class="buttonNormal" /> <i></i>
            <input id="all" name="all" value="<?= $l_all ?>" onclick="searchingAll(1,'all')"
                   class="buttonNormal" type="button" />
        </div>
        <br style="clear: both" />
        <table id="listEmp" class="rf-dt" style="width: 100%;">
            <thead class="rf-dt-thd" >
                <tr class="rf-dt-shdr" >
                    <th width="2%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col" id="no"><label><input type="checkbox" checked="checked" id="check_no"><?= $l_no ?></label></th>
                    <th width="12%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col">
                        <label><input type="checkbox" checked="checked" id="check_employee_name"><a href="#" onclick="sortFullname()"><?= $l_employee_name ?></a>
                        <input type="hidden" id="sortfullname" value="<?= SORT_ASC ?>"/>
                    </label></th>
                    <th width="6%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col" id="birthday"><label><input type="checkbox" checked="checked" id="check_birthday"><?= $l_birthday ?></label></th>
                    <th width="6%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_username">
                        <a href="#" onclick="sortUsername()"> <?= $l_username ?></a>
                        <input type="hidden" id="sortusername" value="<?= SORT_ASC ?>"/>
                    </label></th>
                    
                    <th width="4%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_sex"><?= $l_sex ?></label></th>
                    <th width="6%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_department"><?= $l_department ?></label></th>
                    <th width="6%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_position"><?= $l_position ?></label></th>
                    <th width="6%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_startwork"><?= $l_startwork ?></label></th>
                    <th width="8%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_cmnd"><?= $l_cmnd ?></label></th>
                    <th width="15%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_address"><?= $l_address ?></label></th>
                    <th width="8%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_mobile"><?= $l_mobile ?></label></th>
                    <th width="12%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><label><input type="checkbox" checked="checked" id="check_totaldays"><?= $l_totaldays ?></label></th>
                    <th width="3%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"></th>
                </tr>
<?php
$i = 0;
$employees = array();
while ($row = mysql_fetch_array($sqlempworking)) {
    $employees[$row[IDEmployee] . '-' . $row[FullName]][] = array($row[IDEmployee], $row[FullName], $row[Birthday], $row[UserName], $row[SexName], $row[DepartmentName], $row[Position], $row[StartDate], $row['CMND'], $row['Address'], $row['Mobile']);
    $mangemployees[] = $row[IDEmployee] . '-' . $row[FullName];
}
$_SESSION['employeeworking']=$employees;
$_SESSION['mangemployeeworking']=$mangemployees;
$mangemployees = ViSortByName($mangemployees);
$j=0;
for ($i = 0; $i < count($mangemployees); $i++) {
	$j++;
	if ( $j > $page_start && $j <= $page_end){
    ?>
                    <tr>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $i + 1; ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: left;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-working.php&id=<?= $employees[$mangemployees[$i]][0][0] ?>" ><?php echo $employees[$mangemployees[$i]][0][1]; ?></a></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][2]; ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-working.php&id=<?= $employees[$mangemployees[$i]][0][0] ?>" ><?php echo $employees[$mangemployees[$i]][0][3] ?></a></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][4] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][5] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][6] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][7] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][8] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][9] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $employees[$mangemployees[$i]][0][10] ?></td>
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col">
    <?php
    //$temp_end=strtotime("now");
    //$temp_start = explode(" ", $row[StartDate]);
    //$temp_start = strtotime($temp_start[0]);
    //echo (floor($temp_end / (60 * 60 * 24)) - floor($temp_start / (60 * 60 * 24)));
    $date1 = $employees[$mangemployees[$i]][0][7];
    $date2 = date("Y-m-d");

    $diff = abs(strtotime($date2) - strtotime($date1));

    $years = floor($diff / (365 * 60 * 60 * 24));
    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

    printf("%d $l_year, %d $l_month, %d $l_date\n", $years, $months, $days);
    ?>
                        </td>
                        
                        <td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col">
                            <a href="#" onclick="if(confirm('<?php echo $l_confirm_deleteproject ?>')){  onclickDelete('<?= $employees[$mangemployees[$i]][0][0] ?>',this); return true;}else{return false;}"><?= $l_delete ?></a></td>
                    </tr>
<?php }
}
?>

                <tr class="trfoot">
                    <td class="rf-dt-ftr-c" scope="colgroup" colspan="13">
                        <div style="text-align: center; width: 100%">
                            <span class="rf-ds">

<?php
if ($number_of_page > 1) {
    if ($page == 1)
        $p = 2;
    else if ($page == $number_of_page)
        $p = $number_of_page - 1;
    else
        $p=$page;
    if ($page != 1) {
        ?>
                                        <a onclick="paging(1,'')" ><span class='button_next'><?php echo "<<<"; ?></span> </a>
                                        <a onclick="paging(<?= ($page - 1) ?>,'')" ><span class='button_next'><?php echo "<<"; ?></span> </a>
                                    <?php
                                    } else {
                                        echo "<span class='button_next'><<<</span>";
                                        echo "<span class='button_next'><<</span>";
                                    }
                                    for ($i = $p - 1; $i <= $p + 1; $i++) {
                                        if ($i <= $number_of_page && $i > 0)
                                            if ($i == $page) {
                                                echo "<span class='button_next' style='color: #000'><i>" . $i . "</i></span>";
                                            } else {
                                                ?>
                                                <a onclick="paging(<?= $i ?>,'')" ><span class="button_next">
                                                <?php
                                                echo $i;
                                                ?>
                                                    </span> </a>
                                                        <?php
                                                    }
                                                ?>

                                        <?php
                                    }

                                    if ($page != $number_of_page) {
                                        ?>
                                        <a onclick="paging(<?= ($page + 1) ?>,'')"><span class='button_next'><?php echo ">>"; ?></span> </a>
                                        <a onclick="paging(<?= ($number_of_page) ?>,'')" ><span class='button_next'><?php echo ">>>"; ?></span> </a>
                                    <?php
                                    } else {
                                        echo "<span class='button_next'>>></span> ";
                                        echo "<span class='button_next'>>>></span> ";
                                    }
                                }
                                ?>

                            </span>
                        </div>
                    </td>
                </tr>
            </thead>
        </table>

    </div>
</div>

